// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var css_financingDetailInfo = require("../financing/financingDetailInfo.css");
	var css_deliveryNotice = require("./deliveryNotice.css");
	var css_platform_main = require("./platform_main.css");
	var css_financeApprove = require("./financeApproveDetail.css");
	var css_detail = require("./pickupDetail.css");
	var js_platform_main = require("./platform_main.js");
	var valuationCal = require("valuationCal");
	var storage = window.sessionStorage;
	var tools = require("tools");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var left;
	var financeId;
	var financeAuditId;

	var init = function(page) {
		//		financeId = page.params.financeId || base.getUrlParam("financeId");
		financeAuditId = page.params.financeAuditId;
		tools.inquireCommonSelectorByStringList(15, $("#insuranceCompany"));
		tools.inquireCommonSelectorByStringList(16, $("#insuranceType"));
		initEvent();
		router.init();
		databind.fillInput1($("#financeApproveDetail"), {
			action: "inquireFinanceAuditDetail.json?financeAuditId=" + financeAuditId,
			async: false
		}, function(){
			if($("#approveDetailmaterials").val()!=""){
				$.each($("#approveDetailmaterials").val().split(','), function() {
					var str = '<div class="showAuditIntroDiv"><a target="_blank" class="mas" fileUuid="' + this + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + this + '" class="showAuditIntro">' + tools.inquireFile(this) + '</div>'
					$("#financeApproveDetail .fs").append(str);
				});
			}
		});
		financeId = $("#financeApproveDetail .financeId").val();
		inquireFinanceApplicationDetail();
		databind.disableInput($("#financeApproveDetail"));
	};
	var initEvent = function() {
			//若未登录，则返回登陆页面
			if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
				top.location.href = "../login/login.html";
			}

			//点击首页
			$(".platform_main").unbind("click").click(function() {
				top.location.href = "../main/platform_main.html";
			});

			//点击证明材料
			$(".evidentiaryCategorys1").on("click", function() {
				router.route("main/financingDetailEvidentiaryCategorys1.html?financeId=" + financeId + "&financeAuditId=" +financeAuditId);
			});

			//点击融资申请基本信息
			$(".financeDetailInfo1").on("click", function() {
				router.route("main/financingDetailInfo1.html?financeId=" + financeId + "&financeAuditId=" +financeAuditId);
			});
			
			//点击融资评审
			$(".financingDetailDynamic1").on("click", function() {
				router.route("main/financeApproveDetail.html?financeAuditId=" + financeAuditId);
			});

			$(".button-Yes").on("click", function() {
				if ($(".financeAmountInput").val() <= 0) {
					base.alert(2, "允许融资额度要大于0元");
					return;
				}
				databind.submitDom($(".financeApproveContent"), function() {
					base.alert(1, "融资评审通过成功");
					router.route("main/main_finance.html?left=0&financeState=1", true);
					inquireFinanceLatestAuditDetail();
					databind.disableInput($(".financeApproveContent"));
					$(".button-Yes").remove();
					$(".button-No").remove();
				});
			});

			$(".button-No").on("click", function() {
				$(".financeApproveContent").attr("action", "opposeFinanceApplication");
				databind.submitDom($(".financeApproveContent"), function() {
					base.alert(1, "融资评审拒绝成功");
					router.route("main/main_finance.html?left=0&financeState=1", true);
				});
			});
			//查看企业详情
			$(".tab1").on("click", ".enterpriseDetail", function() {
				var enterpriseId = $("#enterpriseId").val();
				//				base.openUrl("../client/clientManage_details.html?enterpriseId=" + enterpriseId,"new");
				//			router.route("client/clientManage_details.html?enterpriseId=" + enterpriseId, true);
				storage.setItem("enterpriseId", enterpriseId);
				window.open("../client/clientManageDetailIntact.html");
			});
		}
		//查询融资评审信息
	var inquireFinanceLatestAuditDetail = function() {
			base.fnAjax({
				action: "inquireFinanceLatestAuditDetail.json",
				oData: {
					financeId: financeId
				}, //传递参数
				fnSuccess: function(data) {
					if (data.status == 200) {
						databind.fillForm($(".financeApproveContent"), data.datas[0]);
						$(".financeNumber").html(data.datas[0].financeNumber);
					} else {
						base.alert(2, "信息获取失败");
					}
				}
			});
		}
		//查询融资基本信息
	var inquireFinanceApplicationDetail = function() {
			base.fnAjax({
				action: "inquireFinanceApplicationDetail.json",
				oData: {
					financeId: financeId
				}, //传递参数
				fnSuccess: function(data) {
					if (data.status == 200) {
						databind.fillForm($(".financeApproveContent"), data.datas[0]);
						$(".interest").val(data.datas[0].interest);
						$(".financeNumber").html(data.datas[0].financeNumber);
						var html;
						var bulkstocksTpl = $("#bulkstocksTpl").html();
						if (bulkstocksTpl != undefined) {
							html = juicer(bulkstocksTpl, data.datas[0]);
						}
						$(".bulkstocks").append(html);
						var financeDetailApproveTpl = $("#financeDetailApproveTpl").html();
						if (financeDetailApproveTpl != undefined) {
							html = juicer(financeDetailApproveTpl, data);
						}
						$(".pledging_goods_Approve").append(html);

						$(".applicationAmount").html(data.datas[0].applicationAmount);
						//重新估值
						$(".financeApproveValuation").unbind("click").click(function() {
							var unit = "";
							var quantity = 0;
							if ($(this).attr("measurement") == 1) {
								unit = "体积：";
								quantity = $(this).attr("volume");
							} else if ($(this).attr("measurement") == 2) {
								unit = "重量：";
								quantity = $(this).attr("weight");
							}
							valuationCal.showCal("货物估值", $(this).attr("bulkstockInfoId"), unit, quantity, $(this).parent().find(".evaluateValuation"), $(this).parent().find(".evaluatePrice"), $(this).attr("conversionFactor"));
						});
					} else {
						base.alert(2, "信息获取失败");
					}
				}
			});
		}
		//最新商品采集价格列表
	var inquireBulkstockLatestCollectionList = function(bulkstockInfoId) {
		base.fnAjax({
			action: "inquireBulkstockLatestCollectionList.json",
			async: false,
			oData: {
				pageSize: pageSize,
				pageIndex: pageIndex,
				bulkstockInfoId: bulkstockInfoId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#bulkstockCollectionListTpl").html();
					var html = juicer(tpl, data);
					$("#bulkstockCollectionListBody").html(html);

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#valuationPagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireBulkstockLatestCollectionList();
						});

					$(".priceCheck").on("click", function() {
						$(".valuationRegionPrice").val(($(this).parents("tr").find(".price").html()).toFixed(6));
						var valuationRegionPrice = $(".valuationRegionPrice").val();
						var valuationQuantity = $(".valuationQuantity").val();
						if (valuationRegionPrice == "") {
							valuationRegionPrice = 0;
						}
						if (valuationQuantity == "") {
							valuationQuantity = 0;
						}
						$("#valuationPrice").html((valuationRegionPrice * valuationQuantity).toFixed(2));
					});
				} else {
					//                  base.alert(2,"信息获取失败");
				}
			}
		});
	};
	return {
		init: init
	};
});